<h1 id="f_margin">Stores</h1>
<?php buzzr_fb_like();?>
<div class="clear"></div>
<?php 
$store_list = new Pod('brand');
$letter = pods_url_variable('filter', 'get');
if($letter == '0-9'){
  $where = "t.name REGEXP '^[0-9][0-9]'";
}
else{
  
  if($letter == 'all'){
    $letter = '';
  }
  $where = "t.name LIKE '". mysql_real_escape_string($letter) . "%' AND (campaigns.coupon = 1 OR campaigns.quickcamp = 1 OR (campaigns.daily = 1 AND NOT ISNULL(t.webpage)))";
}
$store_list->findRecords('t.id DESC', 15, $where);

$letters = range('A', 'Z');
$links .= '<div id="letter_links">Filter by:  <br /><a href="?filter=all" rel="nofollow">ALL</a>&nbsp;|&nbsp;';
// Loop through the array of letters
for($i = 0; $i < 26; $i++) {
  $spacer = ($i == 0)? '' : '&nbsp;|&nbsp;';
 $links .= $spacer . '<a rel="nofollow" href="?filter=' . $letters[$i] . '">' . $letters[$i] . '</a>';
}
$links.='&nbsp;|&nbsp;<a rel="nofollow" href="?filter=0-9">0-9</a></div>';
echo $links;
?>

<div id="title">Featured Brands: </div>
<?php
$featured_brands= new Pod('brand');
$featured_brands->page = 1;
$featured_brands->search = false;
$featured_brands->findRecords('id DESC', 5, "t.featured = 1");
while ($featured_brands->fetchRecord())
{
	$brand_name = $featured_brands->get_field('name');
	$image = $featured_brands->get_field('logo');
	$small_image =  wp_get_attachment_image_src($image[0][ID], 'thumbnail', false);
	$small_image = $small_image[0];
	$brand_url =  $featured_brands->get_field('brandurl');
?>
	<span class="logo">
		<a href="<?php echo BUZZR_URL.'/brands/'.$brand_url; ?>">

	<?php if(!empty($small_image)){ ?>
		<img src="<?php echo $small_image; ?>" alt="<?php echo $brand_name; ?>" />		
	<?php }else{ echo $brand_name;  } ?>
		</a>
	</span>
	
<?php } ?>
<div class="clear"></div>

<div id="list_wrap">
		<ul class="store_list">
		
		<?php
		while($store_list->fetchRecord()){
			$brand_name = $store_list->get_field('name');
			$brand_url = BUZZR_URL.'/brands/'.$store_list->get_field('brandurl');
			$brand_id = $store_list->get_field('id');
			$image = $store_list->get_field('logo');
			$small_image =  wp_get_attachment_image_src($image[0][ID], 'thumbnail', false);
			$small_image = $small_image[0];
			
			$Record = new Pod('deal');
			$where = "t.camp_end >= ".fix_date()." && brand.id='".$brand_id."'";
			$Record->findRecords('id DESC', -1, $where);
			$active_offers = $Record->getTotalRows();
		?>
			<li>
	            <div class="stores_deal">
	              <div class="stores_deal_list">
	                <div class="stores_images">
	                	<?php if(!empty($small_image)){ ?>
	                		<a href="<?php echo $brand_url; ?>"><img src="<?php echo $small_image; ?>" /></a>
	                	<?php }else{ ?>
	                	<a href="<?php echo $brand_url; ?>">
	                	<div id="fallback">
	                		<p>
	                			<?php echo $brand_name; ?>
	                		</p>
	                	</div>
	                	</a>
	                	<?php } ?>
	                </div>
	              </div>
	              
	              <div class="stores_deal_list_details">
	              	<p><a href="<?php echo $brand_url; ?>"><?php echo $brand_name; ?></a></p>
	              	<p>Active offers: <?php echo $active_offers; ?> </p>
	              	<p class="store_more">See all <a href="<?php echo $brand_url; ?>"><?php echo $brand_name; ?></a> Deals </p>
	              </div>
	              <div class="clear"></div>
	             </div>
	        </li>
	     <?php } ?>
	     
	     </ul>
</div>